import os

class XMLValidator:

    def __init__(self, javaPath, jarPath):
        self.javaPath = javaPath
        self.jarPath = jarPath

    def validate(self, xmlString, xsdFilePath):
        command = "%s -cp \"%s\" business.XMLValidate piped" %(self.javaPath, self.jarPath)
        print command 
        p = os.popen3(command)
        p[0].write(xmlString)
        p[0].write("\0")
        p[0].write(xsdFilePath)
        p[0].write("\0")
        p[0].close()
        
        resultLength = int(p[1].readline().split("\n")[0])
        result = p[1].read(resultLength)
        return result
        
if __name__ == "__main__":
    xsdFilePath=r"c:/Plone3_Instance4080/Products/ZMetadata/standards/dublin-core/dc.xsd"
    #xmlFilePath=r"c:/temp/iso_19139_master.xml"
    #xmlFilePath=r"c:/temp/fgdc/veg-model-metadata.xml"
    xmlFilePath=r"c:/temp/sample_metadata/test_dc.xml"

    # load the test xml file
    xmlFile = file(xmlFilePath)
    xmlFile.seek(0,2)
    xmlFileSize = xmlFile.tell()
    xmlFile.seek(0)
    xmlString = xmlFile.read(xmlFileSize)
    xmlFile.close()
    classpath = r"c:/Plone3_Instance4080/Products/ZMetadata/xercesImpl.jar;c:/Plone3_Instance4080/Products/ZMetadata/xml-apis.jar;c:/Plone3_Instance4080/Products/ZMetadata/XMLValidator.jar;"
    #classpath = r"c:/Plone3_Instance4080/Products/ZMetadata/XMLValidator.jar;c:/Plone3_Instance4080/Products/ZMetadata/XMLValidator.jar;"
    validator = XMLValidator("java", classpath)
    result = validator.validate(xmlString, xsdFilePath)
    print result





